/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: JoinGameScreen.java 639 2010-07-30 05:53:30Z Harry $
 *   $LastChangedDate: 2010-07-30 05:53:30 +0000 (Fri, 30 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.screen;

import battlefield.resource.Graphics;
import battlefield.entity.Game;
import battlefield.entity.Player;
import battlefield.ui.component.NormalWhiteButton;
import battlefield.util.ExceptionUtils;
import battlefield.util.ComponentDecorator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.List;
import javax.naming.NamingException;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Yong Peng
 */
public class JoinGameScreen extends Screen implements ActionListener {

    JLabel joinGameLab;
    JLabel ipAddressLabel;
    JLabel gameLabel;
    JTextField ipAddressText;
    JComboBox gameComboBox;
    JButton nextBut;
    JButton backBut;
    JButton joinGameBut;
    List<Game> gameList;

    public JoinGameScreen() {
        initComponents();
    }

    private void initComponents() {
        joinGameLab = new JLabel(new ImageIcon(Graphics.SCREEN_TITLE_JOIN_GAME.getImage()));
        ipAddressLabel = ComponentDecorator.decorateLabel(ipAddressLabel, "Your IP Address");
        gameLabel = ComponentDecorator.decorateLabel(gameLabel, "Game List");
        ipAddressText = ComponentDecorator.decorateTextfield(ipAddressText, 15, "localhost");
        backBut = new NormalWhiteButton("Back", this);
        nextBut = new NormalWhiteButton("Next", this);
        joinGameBut = new NormalWhiteButton("Join Game", this);
        joinGameBut.setVisible(false);
        gameComboBox = new JComboBox();
        gameComboBox.setVisible(false);
        gameLabel.setVisible(false);
        this.setBackgroundImage(Graphics.BACKGROUND_MISC.getImage());
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        layout.setHorizontalGroup(
                layout.createSequentialGroup().addGap(50, 50, 50).addGroup(
                layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(joinGameLab).addGroup(layout.createSequentialGroup().addComponent(ipAddressLabel).addComponent(ipAddressText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addComponent(gameLabel).addComponent(gameComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addComponent(backBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(nextBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(joinGameBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))));
        layout.linkSize(ipAddressLabel, gameLabel);
        layout.setVerticalGroup(
                layout.createSequentialGroup().addGap(50, 50, 50).addComponent(joinGameLab).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(ipAddressLabel).addComponent(ipAddressText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(gameLabel).addComponent(gameComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(backBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(nextBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(joinGameBut, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)));
    }

    private void initComboBox() {

        try {
            for (Game game : getLogic().getGames()) {
                gameComboBox.addItem(game.getId());
                gameComboBox.setVisible(true);
            }
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(this, ExceptionUtils.getMessage(ex));
        }
    }

    /* Begin Region: State Pattern */
    @Override
    public Screen chooseMap() {
        try {
            Player player = getLogic().registerPlayer();
            player.setName(getSession().getPlayerName());
            getLogic().changePlayerName(player.getId(), player.getName());
            getSession().setPlayer(player);
            getLogic().joinGame(getPlayer().getId(), (Integer) gameComboBox.getSelectedItem());
        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(this, ExceptionUtils.getMessage(ex));
        }
        return new MapChooserScreen();
    }

    @Override
    public Screen back() {
        getNetwork().stopServer();
        return new GameMenuScreen();
    }
    /* End Region: State Pattern */

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(nextBut)) {
            try {
                getSession().setIpAddress(ipAddressText.getText());
                getNetwork().startClient(ipAddressText.getText());
                initComboBox();
                nextBut.setVisible(false);
                joinGameBut.setVisible(true);
                gameLabel.setVisible(true);
            } catch (NamingException ex) {
                JOptionPane.showMessageDialog(this, ExceptionUtils.getMessage(ex));
            }
        } else if (e.getSource().equals(joinGameBut)) {
            getGUI().chooseMap();
        } else if (e.getSource().equals(backBut)) {
            getGUI().back();
        }
    }
}
